package com.example.lib_common.utils

import android.R.attr.radius
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.util.TypedValue
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.databinding.BindingAdapter

fun View?.hideSoftKeyboard() {
    if (this == null) return
    this.clearFocus()
    (this.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(
        this.windowToken, 0
    )
}

fun View?.shouldHideInput(ev: MotionEvent): EditText? {
    if (this == null) return null
    if (ev.action == MotionEvent.ACTION_DOWN && this is EditText) {
        // 是输入框
        val leftTop = intArrayOf(0, 0)
        // 获取输入框当前的位置
        this.getLocationInWindow(leftTop)
        val top = leftTop[1]
        val bottom = top + this.height
        // 触摸位置不在输入框范围内，需要隐藏
        if (!(ev.y > top && ev.y < bottom)) {
            return this
        }
    }
    return null
}


//自定义shape_radius、shape_solidColor字段  即圆角和填充颜色
@BindingAdapter(value = ["shape_color", "shape_radius"], requireAll = false)
fun View.setViewBackground(solidColor: Int = Color.TRANSPARENT, shapeRadius: Int = 0) {
    val drawable = GradientDrawable()
    drawable.cornerRadius = context.dp2px(shapeRadius.toFloat())
    drawable.setColor(solidColor)
    clipToOutline = true
    background = drawable
}

@BindingAdapter(
    value = ["shape_color",//填充颜色
        "shape_radiusTs", // top-start
        "shape_radiusTe", // top-end
        "shape_radiusBs", // bottom-start
        "shape_radiusBe"  // bottom-end
    ], requireAll = false
)
fun View.setViewBackground(
    solidColor: Int = Color.TRANSPARENT,
    radiusTs: Int = 0,
    radiusTe: Int = 0,
    radiusBs: Int = 0,
    radiusBe: Int = 0,
) {
    val drawable = GradientDrawable()
    drawable.setColor(solidColor)
    val isRtl = layoutDirection == View.LAYOUT_DIRECTION_RTL
    val cornerRadii = if (isRtl) {
        floatArrayOf(
            context.dp2px(radiusTe.toFloat()), // top-left ← top-end
            context.dp2px(radiusTe.toFloat()),
            context.dp2px(radiusTs.toFloat()), // top-right ← top-start
            context.dp2px(radiusTs.toFloat()),
            context.dp2px(radiusBs.toFloat()), // bottom-right ← bottom-start
            context.dp2px(radiusBs.toFloat()),
            context.dp2px(radiusBe.toFloat()), // bottom-left ← bottom-end
            context.dp2px(radiusBe.toFloat())
        )
    } else {
        floatArrayOf(
            context.dp2px(radiusTs.toFloat()), // top-left ← top-start
            context.dp2px(radiusTs.toFloat()),
            context.dp2px(radiusTe.toFloat()), // top-right ← top-end
            context.dp2px(radiusTe.toFloat()),
            context.dp2px(radiusBe.toFloat()), // bottom-right ← bottom-end
            context.dp2px(radiusBe.toFloat()),
            context.dp2px(radiusBs.toFloat()), // bottom-left ← bottom-start
            context.dp2px(radiusBs.toFloat())
        )
    }
    drawable.cornerRadii = cornerRadii
    clipToOutline = true
    background = drawable
}

private fun Context.dp2px(dpValue: Float): Float {
    return TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, dpValue, resources.displayMetrics
    )
}
